<?php
/**
 * +--------------------------------------------------------+
 * @Created by Phpstorm.
 * @Category CollectArticleRepository.php
 * @Depiction
 * @Author hwy <429208948@qq.com>
 * @Date 2022/4/17  17:02
 * +--------------------------------------------------------+
 */

namespace App\Repositories;

use App\Models\CollectArticle;
use Carbon\Carbon;

class CollectArticleRepository extends BaseRepository
{
    protected $collectArticle;

    public function __construct(CollectArticle $collectArticle)
    {
        $this->collectArticle = $collectArticle;
    }

    /**
     * @return array
     * @Depiction
     * @Author hwy <429208948@qq.com>
     * @Date 2022/6/17  19:56
     */
    public function getData()
    {
        $data = $this->collectArticle->query()
            ->select('title')
            ->where('created_at', '>', Carbon::today()->subDays(30))
            ->get();
        if (empty($data)) {
            return [];
        }
        return $data->pluck('title')->toArray();
    }


    public function addData($params)
    {
        $res = $this->collectArticle->query()->insert($params);

        return $res;
    }


    public function getNumber($params = 0)
    {
        $query = $this->collectArticle->query();
        if (!empty($params)) {
            $query = $query->where('source', $params);
        }
        $res = $query->count();;

        return $res;
    }

    public function getList()
    {
        $query   = $this->collectArticle->query();
        $perPage = empty($params['perPage']) ? 10 : $params['perPage'];

        if (!empty($params['field']) && !empty($params['order'])) {
            $query = $query->orderBy($params['field'], $params['order']);
        }


        return $list = $query->orderBy('id', 'desc')->paginate($perPage);
    }


}
